#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase
from elsLibs.ProjectInfo import ProjectInfo

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CfxGuideCurveAttrsCheck(CheckBase):
    def __init__(self):
        super(CfxGuideCurveAttrsCheck, self).__init__()
        self.init_els()

    def init_els(self):
        self.els_project = ProjectInfo()

        # 通过文件名称获取资产信息
        maya_file = mu.sceneName()
        file_name = os.path.basename(maya_file)
        self.asset_short_type = file_name.split("_")[0]
        self.asset_name = file_name.split("_")[1]
        self.asset_type = self.els_project.asset_short_type_data[self.asset_short_type]


    def check_guideCurve_attrs(self):
        """
        描述：guideCurve节点检查的启动函数
        """
        error_nodes = []
        guideCurve_grp = "{0}_{1}_guideCurve_grp".format(self.asset_short_type, self.asset_name)
        if not cmds.objExists(guideCurve_grp):
            error_nodes.append(guideCurve_grp)
            return
        guideCurve_trans_nodes = []
        self.get_guideCurve_trans_nodes(guideCurve_grp, guideCurve_trans_nodes)
        print guideCurve_trans_nodes
        for node in guideCurve_trans_nodes:
            if not self.check_curve_trans_attrs(node):
                error_nodes.append(node)
            elif not self.check_attrs_lock_state(node):
                error_nodes.append(node)

        return error_nodes

    def check_curve_trans_attrs(self, node):
        trans_attrs = cmds.getAttr(node + ".translate")[0]
        rotate_attrs = cmds.getAttr(node + ".rotate")[0]
        scale_attrs = cmds.getAttr(node + ".scale")[0]
        for attr in trans_attrs:
            if attr != 0.0:
                return False
        for attr in rotate_attrs:
            if attr != 0.0:
                return False
        for attr in scale_attrs:
            if attr != 1.0:
                return False
        return True

    def check_attrs_lock_state(self, node):
        check_attrs = ["translateX", "translateY", "translateZ",
                 "rotateX", "rotateY", "rotateZ",
                 "scaleX", "scaleY", "scaleZ"]
        for attr in check_attrs:
            if cmds.getAttr(node + "." + attr, l=True):
                return False
        return True

    def get_guideCurve_trans_nodes(self, node, guideCurve_nodes):
        if cmds.listRelatives(node, c=True, s=True):
            if cmds.nodeType(cmds.listRelatives(node, c=True, s=True)[0]) == "nurbsCurve":
                guideCurve_nodes.append(node)
        elif cmds.listRelatives(node, c=True):
            for sub_node in cmds.listRelatives(node, c=True):
                self.get_guideCurve_trans_nodes(sub_node, guideCurve_nodes)

    def reset_node_attrs(self, node):
        # 取消属性锁定
        check_attrs = ["translateX", "translateY", "translateZ",
                       "rotateX", "rotateY", "rotateZ",
                       "scaleX", "scaleY", "scaleZ"]
        for attr in check_attrs:
            if cmds.getAttr(node + "." + attr, l=True):
                cmds.setAttr(node + "." + attr, l=False)

        # 设置属性值
        trans_attrs = ["translateX", "translateY", "translateZ",
                       "rotateX", "rotateY", "rotateZ"]
        for attr in trans_attrs:
            cmds.setAttr(node + "." + attr, 0.0)
        scale_attrs = ["scaleX", "scaleY", "scaleZ"]
        for attr in scale_attrs:
            cmds.setAttr(node + "." + attr, 1.0)

    @property
    def imagesource_path(self):
        imagesource_path = os.path.join(self.els_project.root_path, "Assets", self.asset_type, self.asset_name, "hair", "sourceimages")
        return os.path.normpath(imagesource_path)

    def check(self):
        result_proc = self.check_guideCurve_attrs()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        try:
            miss_nodes = self.check_guideCurve_attrs()
            for node in miss_nodes:
                self.reset_node_attrs(node)
            return u'Succeed:没有问题\n'
        except:
            self.check()

